// pages/admin/adminplat/adminplat.js
import sendRequest from '../../rpromise/request'

Page({

    /**
     * 页面的初始数据
     */
    data: {
        current: 0,
        tabs: ['未审核', '已审核'],
        index: null,
        order: null,
        date: '2023-01',
        activity: [],
        searchValue: ''
    },
    tabSelect(e) {
        // console.log(e.currentTarget.dataset.id)
        this.setData({
            current: e.currentTarget.dataset.id
        })
    },
    bindDateChange(e) {
        var value = e.detail.value
        this.setData({
            date: value
        })
    },
    searchCoach(e) {
        console.log(e.detail.value)
        this.setData({
            searchValue: e.detail.value,
        })
    },
    LookActivity(e) {
        var i = e.currentTarget.dataset.id
        var acdata = this.data.activity[i]
        acdata.checksign = 'null'
        var adata = JSON.stringify(acdata)
        console.log(adata)
        wx.navigateTo({
            url: '/pages/admin/adminactivity/adminactivity?adata=' + adata,
        })
    },
    addUser(e) {
        var flag = e.currentTarget.dataset.flag
        var index = e.currentTarget.dataset.index
        console.log(index)
        var userdata = {
            tno: '账号',
            password: '密码',
            tname: '昵称',
            td: '学院',
            tphone: '联系电话'
        }
        if (flag != 'new') {
            userdata = this.data.activity[index]
        }
        var data = JSON.stringify(userdata)
        wx.navigateTo({
            url: '/pages/admin/adminuser/adminuser?flag=' + flag + "&userdata=" + data,
        })
    },
    deleteUser(e) {
        var that = this
        var index = e.currentTarget.dataset.index
        var tdata = this.data.activity
        var tno = tdata[index].tno
        console.log(tno)
        wx.showModal({
            title: '提示',
            content: '确定要删除该账号吗？',
            complete: (res) => {
                if (res.cancel) {

                }
                if (res.confirm) {
                    var sql = "DELETE FROM teacher WHERE tno='" + tno + "'"
                    this.sqlRequest(sql).then(res => {
                        console.log(res)
                        if (res.statusCode === 200) {
                            tdata.splice(index, 1)
                            that.setData({
                                activity: tdata
                            })
                            wx.showToast({
                                title: '删除成功',
                                icon: 'success'
                            })
                        } else {
                            wx.showToast({
                                title: '出错了，请重试！',
                                icon: 'error'
                            })
                        }
                    })
                }
            }
        })
    },
    async sqlRequest(sql) {
        const res1 = await sendRequest({
            method: 'POST',
            url: '/manage',
            data: {
                sql: sql
            },
        })
        // console.log(res1)
        return res1
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        wx.setNavigationBarTitle({
            title: options.title,
        })
        this.setData({
            order: options.order,
            activity: JSON.parse(options.activity)
        })
        console.log(this.data.activity)
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})